package sk.tuke.android4all.messenger;

import java.util.ArrayList;
import java.util.List;

import sk.tuke.android4all.controls.CustomFragment;
import sk.tuke.android4all.listeners.SwipeListener;
import sk.tuke.android4all.services.Tts;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnGenericMotionListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MenuFragment extends CustomFragment {
	private static String TAG = "MenuFragment";
	private Activity mActivity = null;
	private String selectedTag = null;
	int touchCounter = 0;
	private final int TOUCH_INTENSITY = 4;
	private SwipeListener mActionListener = null;
	private Tts mSpeechEngine;
	
	public MenuFragment(Activity aActivity_in){
		mActivity = aActivity_in;
		selectedTag = "";
		mSpeechEngine = new Tts(mActivity);
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_menu, container, false);	       
        return rootView;
    }
	
	@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        createUI();
    }
	
	private void createUI() {
		mActionListener = new SwipeListener(this.mActivity) {
	        @Override
	        public void onDoubleTapAction(int count) {
	            Log.i("TAG", "DoubleTapped " + selectedTag);
	            resetTouchCounter();
	            if(selectedTag != null){
	            	FragmentTransaction f = mActivity.getFragmentManager().beginTransaction();
	            	if(selectedTag.equalsIgnoreCase("New Message")){
	            		mSpeechEngine.sayMessage(getResources().getString(R.string.newmessage_intro));
	            		f.replace(R.id.messages_container, new NewMessageFragment(mActivity), NewMessageFragment.class.getName()).commit();
	            	}else if(selectedTag.equalsIgnoreCase("Inbox")){
	            		mSpeechEngine.sayMessage(getResources().getString(R.string.inbox_intro));
	            		f.replace(R.id.messages_container, new ReceivedMessagesFragment(mActivity), ReceivedMessagesFragment.class.getName()).commit();
	            	}else if(selectedTag.equalsIgnoreCase("Outbox")){
	            		mSpeechEngine.sayMessage(getResources().getString(R.string.outbox_intro));
	            		f.replace(R.id.messages_container, new SentMessagesFragment(mActivity), SentMessagesFragment.class.getName()).commit();
	            	}else{
	            		Log.e(TAG, "Unknown tag " + selectedTag);
	            	}
	            }
	        }

			@Override
			public void onZoomInAction() {
				// TODO Auto-generated method stub
				 
			}

			@Override
			public void onZoomOutAction() {
				// TODO Auto-generated method stub
				
			}
		};
		
		getView().setOnTouchListener(mActionListener);
		 
		final TextView newMessageBtn = (TextView) getView().findViewById(R.id.new_message);
		final TextView receivedBtn = (TextView) getView().findViewById(R.id.received_messages);
		final TextView sentBtn = (TextView) getView().findViewById(R.id.sent_messages);

		newMessageBtn.setOnGenericMotionListener(new OnGenericMotionListener(){

			@Override
			public boolean onGenericMotion(View arg0, MotionEvent arg1) {
				mActionListener.resetTouchCounter();
				String textInControl = newMessageBtn.getText().toString();
                if (selectedTag != textInControl && touchCounter>TOUCH_INTENSITY) {
                    touchCounter = 0;
                    mSpeechEngine.sayMessage(textInControl);
                    selectedTag = newMessageBtn.getText().toString();
                }else{
                    touchCounter++;
                }
                return false;
			}});
		
		receivedBtn.setOnGenericMotionListener(new OnGenericMotionListener(){

			@Override
			public boolean onGenericMotion(View arg0, MotionEvent arg1) {
				mActionListener.resetTouchCounter();
				String textInControl = receivedBtn.getText().toString();
                if (selectedTag != textInControl && touchCounter>TOUCH_INTENSITY) {
                    touchCounter = 0;
                    mSpeechEngine.sayMessage(textInControl);
                    selectedTag = receivedBtn.getText().toString();
                }else{
                    touchCounter++;
                }
                return false;
			}});
		
		sentBtn.setOnGenericMotionListener(new OnGenericMotionListener(){

			@Override
			public boolean onGenericMotion(View arg0, MotionEvent arg1) {
				mActionListener.resetTouchCounter();
				String textInControl = sentBtn.getText().toString();
                if (selectedTag != textInControl && touchCounter>TOUCH_INTENSITY) {
                    touchCounter = 0;
                    mSpeechEngine.sayMessage(textInControl);
                    selectedTag = sentBtn.getText().toString();
                }else{
                    touchCounter++;
                }
                return false;
			}});

	}
	
	@Override
	public void returnAction() {
		mActivity.finish();
	}
		
}
